<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Header_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
		if(!isset($_SESSION)){
		    session_start();
		}
    }
    
	/**
     * 未读邮件数量
     */
    function show(){
    	$data = array();
    	$user = $this->session->userdata('zjy_user');
    	if(isset($user['user_id']) && intval($user['user_id']) > 0){
    		$user_id = $user['user_id'];
    		$data = array(
    			'user' => $user,
	    		'total' => 0,//总数
	    		'no_read' => 0//未读数
	    	);
	    	$key = 'not_read_count_'.$user_id;
	    	//$this->mycache->delete($key);//@todo
	    	$num = $this->mycache->get($key);
	    	if($num === false){
	    		$sql = "select count(id) as num from zjy_message where receive_user='{$user_id}' and deltype&1=0 and fstatus='0'";
		    	$query = $this->db->query($sql);
		    	$row = $query->row_array();
		    	$num = $row['num'];
		    	$this->mycache->set($key,$num);
	    	}
	    	
	    	$key2 = 'total_message_'.$user_id;
	    	//$this->mycache->delete($key2);//@todo
	    	$num2 = $this->mycache->get($key2);
	    	if($num2 === false){
	    		$sql = "select count(id) as num from zjy_message where receive_user='{$user_id}' and deltype&1=0";
	    		$query = $this->db->query($sql);
		    	$row = $query->row_array();
		    	$num2 = $row['num'];
		    	$this->mycache->set($key2,$num2);
	    	}
	    	
	    	$data['no_read'] = $num;
	    	$data['total'] = $num2;
    	}
    	return $data;
    }
    
    function user(){
    	$user = array();
    	$user = $this->session->userdata('zjy_user');
    	if(isset($user['user_id']) && intval($user['user_id']) > 0){
    		$user_id = $user['user_id'];
    		$this->load->model('User_model');
    		$user = $this->User_model->get_by_uid($user_id);
    	}
    	return $user;
    }
    
}